<?php


class raportingActions extends sfActions
{	
    public function executeSaveRaport(sfWebRequest $request)
    {
		$this->garden_id=$request->getParameter('id');			
		
		$c=new Criteria();
		//$c->add(gardenPeer::USER_ID,$this->getUser()->getUserId());
		$c->add(GardenPeer::ID,$this->garden_id);
		$this->items_plants=GardenPeer::doSelect($c);
		
		if(!empty($this->items_plants))
		{
			$this->garden_name=$this->items_plants[0]->getName();
		}else{
			//TODO
			return $this->renderText('Nie ma takiego ogrodu');
		}
				
			$c=new Criteria();
			$c->add(GardenItemPeer::GARDEN_ID,$this->garden_id);
			$this->items_plants=GardenItemPeer::doSelect($c);
			
			$list_item=array();
			
			
			if($request->getParameter('sendemail')=='true')
			{
				$c=new Criteria();
				$c->add(gardenItemOrderPeer::GARDEN_ID,$this->garden_id);
				GardenItemOrderPeer::doDelete($c);			
						
				foreach($this->items_plants as $item)
				{
					if($request->getParameter('sd_'.$item->getId())!=null)
					{
						
							$gio=new GardenItemOrder();
							$gio->setGardenItemId($item->getId());
							$gio->setGardenId($this->garden_id);
							$gio->save();	
							$list_item[]=$item->getId();		
					}
				}
				
			}else{
				
				$c=new Criteria();
				$c->add(GardenItemOrderPeer::GARDEN_ID,$this->garden_id);
				$gios=GardenItemOrderPeer::doSelect($c);
				
				foreach($gios as $gio)
				{
					$list_item[]=$gio->getGardenItemId();									
				}
				
			}
			
			$c=new Criteria();
			$categories=CategoryPeer::doSelect($c);
			
			$cats=array();
			
			foreach($categories as $cat)
			{				
				//if($request->getParameter('cat_'.$cat->getId())!=null)				
					$cats[]=$cat;
			}
			
			$aPlantsItemId=array();
			$this->all_items=array();
			
			foreach($cats as $cat)
			{
				$c=new Criteria();
				$c->add(itemPeer::CATEGORY_ID,$cat->getId());
				$c->add(gardenItemPeer::GARDEN_ID,$this->garden_id);
				$c->add(gardenItemPeer::ID,$list_item,Criteria::IN);
				$this->items_plants=gardenItemPeer::doSelectJoinAll($c);				
				if(!empty($this->items_plants))
					$this->all_items[]=array($cat->getName(),$this->items_plants);
					foreach($this->items_plants as $key=>$item)
					{				
						$aPlantsItemId[]=$item->getItemId();
					}
			}
							
			$c=new Criteria();
			$c->add(supplierItemPeer::ITEM_ID,$aPlantsItemId,Criteria::IN);
			$suppliers=supplierItemPeer::doSelectJoinAll($c);
			
			$this->items_supplier=array();
					
			foreach($suppliers as $supplier)
			{
				$this->items_supplier[$supplier->getItemId()]=$supplier;				
			}
					
			$this->sum_price=0;	
			
			foreach($this->all_items as $items)
			{
				foreach($items[1] as $item)
				{
					 $this->sum_price+=$this->items_supplier[$item->getItemId()]->getPrice();
				}
			}	
			
			
	}
	
	public function executeSaveRaportToPdf(sfWebRequest $request)
    {
		$this->garden_name='';
		$garden_id=$request->getParameter('id');
							
		$c=new Criteria();
		$c->add(gardenItemOrderPeer::GARDEN_ID,$garden_id);
		$gios=gardenItemOrderPeer::doSelectJoinAll($c);
		
		$list_item=array();
						
		$categoeies=array();
		
		foreach($gios as $gio)
		{
			$list_item[]=$gio->getGardenItemId();			
		}
		
		$c=new Criteria();
		$cats=categoryPeer::doSelect($c);
		
			$c=new Criteria();
			//$c->add(gardenPeer::USER_ID,$this->getUser()->getUserId());
			$c->add(gardenPeer::ID,$garden_id);			
			$this->items_plants=gardenPeer::doSelect($c);
			
			if(!empty($this->items_plants))
			{
				$this->garden_name=$this->items_plants[0]->getName();
			}else{
				//TODO
				return $this->renderText('Nie ma takiego ogrodu');
			}
					
			
			
			
			$aPlantsItemId=array();
			$this->all_items=array();
			
			foreach($cats as $cat)
			{
				$c=new Criteria();
				$c->add(itemPeer::CATEGORY_ID,$cat->getId());
				$c->add(gardenItemPeer::GARDEN_ID,$garden_id);
				$c->add(gardenItemPeer::ID,$list_item,Criteria::IN);
				$this->items_plants=gardenItemPeer::doSelectJoinAll($c);				
							
				$this->all_items[]=array($cat->getName(),$this->items_plants,$cat->getId());
				foreach($this->items_plants as $key=>$item)
				{				
					$aPlantsItemId[]=$item->getItemId();
				}
			}
							
			$c=new Criteria();
			$c->add(supplierItemPeer::ITEM_ID,$aPlantsItemId,Criteria::IN);
			$suppliers=supplierItemPeer::doSelectJoinAll($c);
			
			$this->items_supplier=array();
					
			foreach($suppliers as $supplier)
			{
				$this->items_supplier[$supplier->getItemId()]=$supplier;				
			}
					
			$this->sum_price=0;	
			
			foreach($this->all_items as $items)
			{
				foreach($items[1] as $item)
				{
					 $this->sum_price+=$this->items_supplier[$item->getItemId()]->getPrice();
				}
			}	
			
			
		$htmlcontent = $this->getPartial('raport',array('sum_price'=>$this->sum_price,'all_items'=>$this->all_items,'items_supplier'=>$this->items_supplier,'garden_name'=>$this->garden_name));
	   
		 $pdf = new mPDF();
		 $pdf->WriteHTML($htmlcontent);
		 $pdf->Output();
		 throw new sfStopException();
		
		
	}
	
	public function executeIndex(sfWebRequest $request)
    { 
		if(!$this->getUser()->isAuthenticated())
 		{
    		 $this->redirect('@sf_guard_signin');			 
		}
		
		$this->garden_id=$request->getParameter('id');
				
		$this->garden_name='';
		
		$c=new Criteria();
		$c->add(gardenPeer::USER_ID,$this->getUser()->getUserId());
		$c->add(gardenPeer::ID,$this->garden_id);
		$this->items_plants=gardenPeer::doSelect($c);
		
		if(!empty($this->items_plants))
		{
			$this->garden_name=$this->items_plants[0]->getName();
		}else{
			//TODO
			return $this->renderText('Nie ma takiego ogrodu');
		}
				
		$c=new Criteria();
		$cats=categoryPeer::doSelect($c);
		
		$aPlantsItemId=array();
		$this->all_items=array();
		
		foreach($cats as $cat)
		{
			$c=new Criteria();
			$c->add(itemPeer::CATEGORY_ID,$cat->getId());
			$c->add(gardenItemPeer::GARDEN_ID,$this->garden_id);
			$this->items_plants=gardenItemPeer::doSelectJoinAll($c);
			$this->all_items[]=array($cat->getName(),$this->items_plants,$cat->getId());
			foreach($this->items_plants as $item)
			{
				$aPlantsItemId[]=$item->getItemId();
			}
		}
						
		$c=new Criteria();
		$c->add(supplierItemPeer::ITEM_ID,$aPlantsItemId,Criteria::IN);
		$suppliers=supplierItemPeer::doSelectJoinAll($c);
		
		$this->items_supplier=array();
				
		foreach($suppliers as $supplier)
		{
			$this->items_supplier[$supplier->getItemId()]=$supplier;
			
		}
				
		$this->sum_price=0;	
		
		foreach($this->all_items as $items)
		{
			foreach($items[1] as $item)
			{
				 $this->sum_price+=$this->items_supplier[$item->getItemId()]->getPrice();
			}
		}	
		
		
		//return $this->renderPartial('raport',array('all_items'=>$this->all_items,'items_supplier'=>$this->items_supplier,'garden_name'=>$this->garden_name));
    }  
}

?>